package fetcher

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"time"
)

var rateLimiter = time.Tick(500 * time.Millisecond)

func Fetch(url string) ([]byte, error) {
	<-rateLimiter
	//resp, err := http.Get(url)

	// 添加user-agent、cookie反爬
	newUrl := strings.Replace(url, "http://", "https://", 1)
	request, err := http.NewRequest(http.MethodGet, newUrl, nil)
	if err != nil {
		return nil, err
	}
	request.Header.Add("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36")
	cookie := "nguv=c_1635376684480628114388617218341564525; _Xdwuv=6179de2d10c83; _Xdwnewuv=1; _PVXuv=6179d02d8cb08; newcar_select_car_price=on; Hm_lvt_53eb54d089f7b5dd4ae2927686b183e0=1635376685,1635560149,1635863455; _locationInfo_=%7Burl%3A%22http%3A%2F%2Fbj.xcar.com.cn%2F%22%2Ccity_id%3A%22475%22%2Cprovince_id%3A%221%22%2C%20city_name%3A%22%25E5%258C%2597%25E4%25BA%25AC%22%7D; place_prid_lin=1; place_crid_lin=475; place_prname=%E5%8C%97%E4%BA%AC%E5%B8%82; place_crname=%E5%8C%97%E4%BA%AC%E8%BD%A6%E5%B8%82; isRemoveCookie=1; fw_exc=1%3A1635864177%3B1%3A1635864178%3B1%3A1635864179%3B1%3A1635892496%3B1%3A1635892497; fw_pvc=1%3A1635863455%3B1%3A1635863472%3B1%3A1635864023%3B1%3A1635892496%3B1%3A1635892509; fw_slc=1%3A1635863473%3B1%3A1635864023%3B1%3A1635892509%3B1%3A1635892542%3B1%3A1635892543; fw_clc=1%3A1635863472%3B1%3A1635892770%3B1%3A1635893921; zg_did=%7B%22did%22%3A%20%2217cc40be4a82bd-0cb69af41af2c1-1f3d6851-13c680-17cc40be4a93ea%22%7D; uv_firstv_refers=https%3A//newcar.xcar.com.cn/; Hm_lpvt_53eb54d089f7b5dd4ae2927686b183e0=1635893923; _Xdwstime=1635893923; zg_8f3d0255011c4bc5bae66beca6584825=%7B%22sid%22%3A%201635892496131%2C%22updated%22%3A%201635893923410%2C%22info%22%3A%201635376686280%2C%22superProperty%22%3A%20%22%7B%5C%22platform_type%5C%22%3A%20%5C%22PC%5C%22%2C%5C%22login_id%5C%22%3A%20null%2C%5C%22project_name%5C%22%3A%20%5C%22XCAR%5C%22%2C%5C%22login_status%5C%22%3A%200%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22%22%2C%22landHref%22%3A%20%22https%3A%2F%2Fnewcar.xcar.com.cn%2F40%2F%22%2C%22zs%22%3A%200%2C%22sc%22%3A%200%2C%22firstScreen%22%3A%201635892496131%7D"
	request.Header.Add("cookie", cookie)
	client := http.Client{}
	resp, err := client.Do(request)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
	}

	// <meta charset="GB2312">    simplifiedchinese.GBK.NewDecoder()
	bodyReader := bufio.NewReader(resp.Body)
	e := determineEncoding(bodyReader)
	utf8Reader := transform.NewReader(bodyReader, e.NewDecoder())

	return ioutil.ReadAll(utf8Reader)
}

func determineEncoding(r *bufio.Reader) encoding.Encoding {
	bytes, err := r.Peek(1024)
	if err != nil {
		log.Printf("Fetcher error: %v", err)
		return unicode.UTF8
	}

	e, _, _ := charset.DetermineEncoding(bytes, "")
	return e
}
